<template>
    <el-dialog title="密码修改" :visible.sync="localVisible" width="30%">
        <el-form :model="pwdForm" :rules="rules" ref="pwdForm" label-width="70px" class="demo-ruleForm">
            <el-form-item label="账号">
                <el-input disabled v-model="pwdForm.username"></el-input>
            </el-form-item>
            <el-form-item label="旧密码" prop="oldPwd">
                <el-input type="password" v-model="pwdForm.oldPwd"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPwd">
                <el-input type="password" v-model="pwdForm.newPwd"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" size="small" @click="submitForm('pwdForm')">
                    确定</el-button>
                <el-button size="small" @click="resetForm('pwdForm')">
                    重置</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>
<script>
import { loginEmp, removeToken } from "@/assets/js/auth";
import Cookies from "js-cookie";
import { ajaxPost, popup } from "@/assets/js/common";

export default {
    name: "EditPwd",
    props: {
        value: { // 改用对象形式声明prop
            type: Boolean,
            default: false
        }
    },

    data() {
        return {
            localVisible: this.visible,
            pwdForm: {},
            rules: {
                newPwd: [
                    {
                        required: true,
                        message: "新密码不能为空",
                        trigger: "blur",
                    },
                    {
                        min: 6,
                        max: 8,
                        message: "新密码长度为6-8位",
                        trigger: "blur",
                    },
                ],
                oldPwd: [
                    {
                        required: true,
                        message: "旧密码不能为空",
                        trigger: "blur",
                    },
                    {
                        min: 6,
                        max: 8,
                        message: "旧密码长度为6-8位",
                        trigger: "blur",
                    },
                ],
            },
        };
    },

    watch: {
        visible(newVal) {
            console.log(newVal);
            this.localVisible = newVal; // 当父组件的值变化时，更新本地状态
        },
        localVisible(newVal) {
            this.$emit('update:visible', newVal); // 触发更新事件
        }
    },

    created() {
        this.init();
        console.log(this.value, 111)
    },

    methods: {
        init() {
            this.pwdForm.username = loginEmp().username;
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$confirm("确定修改密码?", "警示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    })
                        .then(() => {
                            ajaxPost("/personal/edit_pwd", this.pwdForm).then(
                                (res) => {
                                    res = res.data;
                                    if (res.code == 200) {
                                        popup("密码修改成功,请重新登录...");
                                        this.pwdForm = {
                                            username: this.pwdForm.username,
                                        };
                                        Cookies.remove("employee");
                                        removeToken();
                                        this.$router.push("/");
                                    } else {
                                        popup(res.msg, "error");
                                    }
                                }
                            );
                        })
                        .catch(() => {
                            this.$message({
                                type: "info",
                                message: "已取消操作",
                            });
                        });
                }
            });
        },
    },
    
};
</script>
<style>
#edit_pwd {
    width: 50%;
    margin: 50px auto;
    background-color: #ffffff;
    padding: 20px;
}
</style>